What is natural-compare-lite?
The natural-compare-lite npm package is designed for comparing strings in a way that humans might naturally order them. This is particularly useful when sorting lists of strings that include numbers, as it ensures that numerical parts are compared numerically rather than lexicographically. For example, 'item2' will come before 'item10', which is not the case with standard string comparison.
What are natural-compare-lite's main functionalities?
Natural String Comparison
This feature allows for the natural comparison of strings, especially useful for sorting arrays where elements contain numbers. The provided code demonstrates sorting an array of strings in a natural order.
"use strict";\nconst naturalCompare = require('natural-compare-lite');\nconsole.log(['item10', 'item2'].sort(naturalCompare)); // ['item2', 'item10']
Other packages similar to natural-compare-lite
string-natural-compare
This package offers functionality similar to natural-compare-lite, providing natural string comparison capabilities. It differs in implementation details and possibly in performance characteristics, but serves a similar purpose of comparing strings in a human-friendly way.
alphanum-sort
Alphanum-sort is another package that provides natural string sorting capabilities. It allows for additional customization options such as case sensitivity and whether to treat whitespace and punctuation as part of the sorting criteria. This makes it a versatile alternative to natural-compare-lite, depending on the specific needs of a project.
@version 1.4.0
@date 2015-10-26
@stability 3 - Stable
Natural Compare –
Compare strings containing a mix of letters and numbers
in the way a human being would in sort order.
This is described as a "natural ordering".
Standard sorting: Natural order sorting:
img1.png img1.png
img10.png img2.png
img12.png img10.png
img2.png img12.png
String.naturalCompare returns a number indicating
whether a reference string comes before or after or is the same
as the given string in sort order.
Use it with builtin sort() function.
Installation
<script src=min.natural-compare.js></script>
- In node.js:
npm install natural-compare-lite
require("natural-compare-lite")
Usage
var a = ["z1.doc", "z10.doc", "z17.doc", "z2.doc", "z23.doc", "z3.doc"];
a.sort(String.naturalCompare);
a.sort(function(a, b){
return String.naturalCompare(a.toLowerCase(), b.toLowerCase());
})
var a = [ {"street":"350 5th Ave", "room":"A-1021"}
, {"street":"350 5th Ave", "room":"A-21046-b"} ];
a.sort(function(a, b){
return String.naturalCompare(a.street, b.street) || String.naturalCompare(a.room, b.room);
})
var a = [ {"make":"Audi", "model":"A6"}
, {"make":"Kia", "model":"Rio"} ];
a.map(function(car){
car.sort_key = (car.make + " " + car.model).toLowerCase();
})
a.sort(function(a, b){
return String.naturalCompare(a.sort_key, b.sort_key);
})
- Works well with dates in ISO format eg "Rev 2012-07-26.doc".
Custom alphabet
It is possible to configure a custom alphabet
to achieve a desired order.
String.alphabet = "ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy"
["t", "z", "x", "õ"].sort(String.naturalCompare)
String.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"
["Ё", "А", "Б"].sort(String.naturalCompare)
External links
Licence
Copyright (c) 2012-2015 Lauri Rooden <lauri@rooden.ee>
The MIT License